function ajaxFileUpload(input) {

	$("#loading").ajaxStart( function() {
		$(this).show();
	}).ajaxComplete( function() {
		$(this).hide();
	});

	var date = new Date().getTime();

	input = $(input);

	var inputId = input.attr("id");
	var inputValue = input.attr("value");

	var index = inputId.substr(inputId.indexOf("_") + 1, inputId.length);

	var imgShow = $("#imgshow_" + index);

	var hidden = $("#hidden_" + index);
    if (!chk_ext(inputValue)) {
        alert("不支持此格式！");
        return false;
    }else{
    	$.ajaxFileUpload( {
    		url : '/meimei/json/upload.action' + '?dateTime=' + date,
    		secureuri : false,
    		fileElementId : inputId,
    		dataType : 'json',
    		success : function(data, status) {
    			$("#imgshow_" + index).attr("src", data.tempUrl);
    			$("#hidden_" + index).attr("value", data.tempUrl);
    		},
    		error : function(data, status, e) {
    		}
    	});
    }

	return false;

}
function ajaxMultipleFileUpload(input){
    $("#loading").ajaxStart(function(){
        $(this).show();
    }).ajaxComplete(function(){
        $(this).hide();
    });
    var date = new Date().getTime();
    
    input = $(input);
    var inputId = input.attr("id");
    var inputValue = input.attr("value");
    var count = 1;
    $(".multiple_img").each(function(){
        if (this.src.indexOf("pic_upload_default.jpg") < 0) {
            count++;
        }
    });
    if (count <= 4) {//页面只许传6张
        if (!chk_ext(inputValue)) {
            alert("不支持此格式！");
            return false;
        }
        else {
            $.ajaxFileUpload({
            	url : '/meimei/json/upload.action' + '?dateTime=' + date,
                secureuri: false,
                fileElementId: inputId,
                dataType: 'json',
                success: function(data, status){
                    $("#imgshow_" + count).attr("src", data.tempUrl);
                    $("#hidden_" + count).attr("value", data.tempUrl);
                },
                error: function(data, status, e){
                }
            });
        }        
    }
    return false;
}
//移除图片 未做物理删除 
function removePic(index){
    var index_num = new Number(index);
    for (var g_index = index_num; g_index < 4; g_index++) {
        var new_hidden_value = $("#hidden_" + (g_index + 1)).val();
        var new_src_value = $("#imgshow_" + (g_index + 1)).attr("src");
        var old_hidden_value = $("#hidden_" + g_index).val();
        var old_src_value = $("#imgshow_" + g_index).attr("src");
        
        $("#imgshow_" + g_index).attr("src", new_src_value);
        $("#hidden_" + g_index).attr("value", new_hidden_value);
        $("#imgshow_" + (g_index + 1)).attr("src", old_src_value);
        $("#hidden_" + (g_index + 1)).attr("value", old_hidden_value);
        
    }
    $("#imgshow_4").attr("src", "/static/plugins/fileupload/pic_upload_default.jpg");
    $("#hidden_4").attr("value", "");
}
function ltrim(s){
    return s.replace(/^\s*/, "");
}

// 去右空格 
function rtrim(s){
    return s.replace(/\s*$/, "");
}

// 去左右空格 
function trim(s){
    return rtrim(ltrim(s));
}

//获取文件扩展名 
function get_ext(f_path){
    var ext = '';
    
    if (f_path != null && trim(f_path).length > 0) {
        f_path = trim(f_path);
        ext = f_path.substring(f_path.lastIndexOf(".") + 1, f_path.length);
    }
    
    return ext;
}

//验证文件扩展名 
function chk_ext(f_path){
    var ext = get_ext(f_path);
    
    //根据需求定制 
    var accept_ext = new Array("BMP", "JPEG", "GIF", "PSD", "PNG", "TIFF", "TGA", "EPS", "JPG");
    
    var flag = false;
    
    if (ext != '') {
        for (var i = 0; i < accept_ext.length; i++) {
            if (ext.toUpperCase() == accept_ext[i]) 
                flag = true;
        }
    }
    return flag;
}